/*
	The MIT License

	Copyright (c) 2010 IFMO/GameDev Studio

	Permission is hereby granted, free of charge, to any person obtaining a copy
	of this software and associated documentation files (the "Software"), to deal
	in the Software without restriction, including without limitation the rights
	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the Software is
	furnished to do so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in
	all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
	THE SOFTWARE.
*/


#pragma once

/*-----------------------------------------------------------------------------
	Physics interface :
-----------------------------------------------------------------------------*/

typedef HardRef<class IPhysics>			IPxPhysics;
typedef HardRef<class IPhysEntity>		IPxPhysEntity;
typedef HardRef<class IPhysCharacter>	IHrPhysCharacter;
typedef HardRef<class IPhys6DOF>		IPxPhys6DOF;

enum EPhysAddForceFlags {
		PHYS_LOCAL_POINT	=	0x0001,
		PHYS_LOCAL_FORCE	=	0x0002,
		PHYS_IMPULSE		=	0x0004,
	};


class IPhysEntity : public ICoreObject {
	public:
		//	commit entity state :
		virtual void		Spawn				( void ) = 0;
		
		//	entity state :
		virtual void		SetMesh				( const IPxTriMesh mesh ) = 0;
		virtual void		SetMass				( float mass ) = 0;
		virtual float		GetMass				( void ) const = 0;
		virtual void		SetInertia			( const EVector &inertia ) = 0;
		virtual EVector		GetInertia			( void ) const = 0;
										
		virtual void		GetPose				( EPoint &position, EQuaternion &orient ) const = 0;
		virtual void		SetPose				( const EPoint &position, const EQuaternion &orient ) = 0;
		
		//	entity type :		
		virtual void		SetDynamic			( void ) = 0;
		virtual void		SetKinematic		( void ) = 0;
		virtual void		SetStatic			( void ) = 0;
		virtual void		SetTrigger			( /* Trigger callback or NULL */ ) = 0;

		//	entity dynamic :		
		virtual void		ZeroForces			( void ) = 0;
		virtual void		AddForce			( const EVector &force ) = 0;
		virtual void		AddTorque			( const EVector &torque ) = 0;
		virtual void		AddForceAtPos		( const EVector &force, const EPoint &point ) = 0;
		virtual void		AddForceAtLocalPos	( const EVector &force, const EPoint &point ) = 0;
		
		virtual void		GetTotalForces		( EVector &force, EVector &torque ) const = 0;

		//	getting attributes :
		virtual EVector		GetLocalPointVelocity	( const EPoint &point ) const = 0;
		virtual EVector		GetCMOffset			( void ) const = 0;

		virtual EVector		GetLinearVelocity	( void ) const = 0;
		virtual void		SetLinearVelocity	( const EVector& velocity ) = 0;

		virtual EVector		GetAngularVelocity	( void ) const = 0;
	};


class IPhys6DOF : public ICoreObject {
	public:
		virtual void	SetAngularLowerLimit	( float twist, float swing_y, float swing_z ) = 0;
		virtual void	SetAngularUpperLimit	( float twist, float swing_y, float swing_z ) = 0;
		
		virtual void	SetAngularMotor			( uint index, bool enable, float target_velocity, float max_force ) = 0;
		virtual void	SetLinearMotor			( uint index, bool enable, float target_velocity, float max_force ) = 0;
		
		virtual void	GetTranslation			( float &dx, float &dy, float &dz ) const = 0;
		virtual void	GetAngles				( float &twist, float &swing_y, float &swing_z ) const = 0;
	};


class IPhysCharacter : public ICoreObject {
	public:
		virtual void		Move				( const EVector &dir, bool &floor, bool &ceil, bool &walls ) = 0;
		virtual void		GetPose				( EPoint &position, EQuaternion &orient ) const = 0;
		virtual void		SetPose				( const EPoint &position, const EQuaternion &orient ) = 0;
	};


class IPhysics : public ICoreObject {
	public:
		virtual void				RunSimulation		( uint dtime ) =  0;
		virtual void				DebugRender			( IPxDVScene dbg_scene ) = 0;

		virtual void				SetGravity			( const EVector &gravity ) = 0;
		virtual EVector				GetGravity			( void ) const = 0;
		
		virtual IPxPhysEntity		AddEntity			( void ) = 0;
		virtual IPxPhys6DOF			Add6DOF				( IPxPhysEntity a, IPxPhysEntity b, EMatrix &basis_a, const EMatrix &basis_b ) = 0;
		virtual IPxPhys6DOF			Add6DOF				( IPxPhysEntity b, const EMatrix &basis_b ) = 0;
		virtual IHrPhysCharacter	AddCharacter		( float heigth, float radius, float climbing_slope, float stair_step ) = 0;
	};